<?php


namespace App\Dao;


use App\Model\Percentage;

class PercentageDao extends AbstractDao
{
    public function where(array $where): \Hyperf\Database\Model\Builder
    {
        $query = Percentage::query();
        foreach ($where as $k => $v) {
            if (is_array($v)) {
                $query->where($k, $v[0], $v[1]);
            } else {
                $query->where($k, $v);
            }
        }
        return $query;
    }

    /**
     * 按条件检索数据
     * @param $where
     * @param array $page
     * @return \Hyperf\Contract\LengthAwarePaginatorInterface
     */
    public function getPercentageQuery($where,array $page)
    {
        if (isset($where['start_date']) && isset($where['end_date'])){
            $start_date = $where['start_date'];
            $end_date = $where['end_date'];
            unset($where['start_date']);
            unset($where['end_date']);
            return Percentage::query()
                ->where($where)
                ->whereBetween('created_at',[$start_date,$end_date])
                ->orderBy('id','DESC')
                ->paginate($page['per_page'],['*'],'page',$page['page']);
        }else{
            return $this->where($where)
                ->orderBy('id','DESC')
                ->paginate($page['per_page'],['*'],'page',$page['page']);
        }
    }


}